<?php


namespace app\common\model\store;


use app\common\model\BaseModel;

class StoreNotice extends BaseModel
{
    protected $name = 'store_notice';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
//    protected $deleteTime = 'deletetime';
    //   protected $hidden = ['updatetime','deletetime'];

    // 追加属性
    protected $append = [
        'type_text',
    ];

    public function wxapp(){
        return $this->belongsTo('app\common\model\Wxapp','wxapp_id','wxapp_id');
    }

    public function getTypeList()
    {
        return ['1' => '系统消息', '2' => '用户通知'];
    }
    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function add($storeId,$type,$item_id,$title,$content, $ext = [])
    {

        return  $add = self::create([
            'wxapp_id' =>$storeId,
            'type'     => $type,
            'item_id'  => $item_id,
            'title'    => $title,
            'content'  => $content,
            'ext'      => $ext
        ]);

    }



    public static function  getUserNotice($uid){
        return  self::where(['wxapp_id'=>$uid,'status'=>0])->order('create_time desc')->select();
    }

    public static function  getUserNoticeInfo($id){
        $statedata = array('status'=>1);
        return  self::where(array('id'=>$id))->setField($statedata);
    }


    public function getTypeAttr($value){
        $status = [1 => '系统消息', 2 => '用户通知'];
        return ['text' => $status[$value], 'value' => $value];
    }

    public function getIsSeeAttr($value){
        $status = [0 => '未查看', 1 => '已查看'];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 详情
     * @param array|int $where
     * @param array $with
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($where, $with = [])
    {
        is_array($where) ? $filter = $where : $filter['id'] = (int)$where;

        return self::get($filter, $with);
    }

    public function edit(){

    }

    public function del($id){
        return $this->transaction(function ($id) {
            //删除
            return self::save(['is_delete' => 1],['id'=>$id]);
        });
    }

    /**
     * 获取全部商户消息列表
     * @param $param
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getAllList($param)
    {
        // 商品列表获取条件
        $params = array_merge([
            //  'status' => -1,         // 订阅状态
            'see'    => -1,         // 阅读状态
            'search' => '',         // 搜索关键词
            'listRows' => 15,       // 每页数量
        ], $param);

        // 筛选条件
        $filter = [];
        // $params['status'] > 0 && $filter['status'] = $params['status'];
        $params['see'] > -1 && $filter['is_see'] = $params['see'];
        if (isset($query['search']) && !empty($query['search'])) {
            $this->where('wxapp.wxapp_name', 'like', '%' . trim($query['search']) . '%');
        }
        // 起始时间
        !empty($params['start_time']) && $this->where('store.create_time', '>=', strtotime($params['start_time']));
        // 截止时间
        !empty($params['end_time']) && $this->where('store.create_time', '<', strtotime($params['end_time']) + 86400);
        // 执行查询
        return  $this->alias('store')
            ->join('wxapp', 'wxapp.wxapp_id =store.wxapp_id')
            ->where($filter)
            ->order('store.create_time desc')
            ->paginate($params['listRows'], false, [
                'query' => \request()->request()
            ]);


    }

}